package com.banciyuan.bcywebview.biz.share.block;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.UserApi;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by squall on 2016/3/7.
 */
public class BlockHelper {

    private IBlockHelper iBlockHelper;
    private Context mContext;
    private RequestQueue mQueue;

    private boolean baning;

    public BlockHelper(IBlockHelper iBlockHelper, Context mContext) {
        this.iBlockHelper = iBlockHelper;
        this.mContext = mContext;
        this.mQueue = VolleyQueue.getRquest(mContext);
    }

    public void doBlock(String token, String uid) {
        if (baning) return;
        baning = true;
        String url = HttpUtils.BASE_URL + UserApi.getDoBlock();
        List<BcyPair> pairs = new ArrayList<BcyPair>();
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, token));
        pairs.add(new BcyPair(HttpUtils.BID, uid));
        HashMap<String, String> map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iBlockHelper.getSucc(s);
                } else {
                    iBlockHelper.getFail();
                }
                baning = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iBlockHelper.getFail();
                baning = false;
            }
        };

        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);

    }

    public void unBlock(String token, String uid) {
        if (baning) return;
        baning = true;
        String url = HttpUtils.BASE_URL + UserApi.getUnBlock();
        List<BcyPair> pairs = new ArrayList<BcyPair>();
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, token));
        pairs.add(new BcyPair(HttpUtils.BID, uid));
        HashMap<String, String> map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iBlockHelper.getSucc(s);
                } else {
                    iBlockHelper.getFail();
                }
                baning = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iBlockHelper.getFail();
                baning = false;
            }
        };

        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);
    }

    public void getBlockList(String token) {
        String url = HttpUtils.BASE_URL + UserApi.getBlockList();
        List<BcyPair> pairs = new ArrayList<BcyPair>();
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, token));
        HashMap<String, String> map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iBlockHelper.getSucc(s);
                } else {
                    iBlockHelper.getFail();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iBlockHelper.getFail();
            }
        };

        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);
    }

    public void getBlockCircleList(String token) {
        String url = HttpUtils.BASE_URL + UserApi.banKeywordsList();
        List<BcyPair> pairs = new ArrayList<BcyPair>();
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, token));
        HashMap<String, String> map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iBlockHelper.getSucc(s);
                } else {
                    iBlockHelper.getFail();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iBlockHelper.getFail();
            }
        };

        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);
    }

    public void addBlockCircle(String token, String id, String type) {
        if (baning) return;
        baning = true;
        String url = HttpUtils.BASE_URL + UserApi.addBanKeywords();
        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, token));
        pairs.add(new BcyPair(HttpUtils.ID, id));
        pairs.add(new BcyPair(HttpUtils.TYPE, type));
        HashMap<String, String> map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iBlockHelper.getSucc(s);
                } else {
                    iBlockHelper.getFail();
                }
                baning = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iBlockHelper.getFail();
                baning = false;
            }
        };

        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);
    }

    public void delBlockCircle(String token, String id, String type) {
        if (baning) return;
        baning = true;
        String url = HttpUtils.BASE_URL + UserApi.delBanKeywords();
        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, token));
        pairs.add(new BcyPair(HttpUtils.ID, id));
        pairs.add(new BcyPair(HttpUtils.TYPE, type));
        HashMap<String, String> map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iBlockHelper.getSucc(s);
                } else {
                    iBlockHelper.getFail();
                }
                baning = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iBlockHelper.getFail();
                baning = false;
            }
        };

        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);
    }
}
